<?php

class foreground {
    public $db, $memberinfo;
    private $_member_modelinfo;

    public function __construct() {
        self::check_ip();
        $this->db = pc_base::load_model('member_model');
        //ajax验证信息不需要登录
        if (substr(ROUTE_A, 0, 7) != 'public_') {
            self::check_member();
        }
    }

    /**
     * 判断用户是否已经登陆
     */
    final public function check_member() {
        $phpcms_auth = param::get_cookie('auth');
        if (ROUTE_M == 'member' && ROUTE_C == 'index' && in_array(ROUTE_A, array('login', 'register', 'repwd', 'send_newmail'))) {
            if ($phpcms_auth && ROUTE_A != 'mini') {
                success_m('登录成功', 'index.php?m=member&c=index');
            } else {
                return true;
            }
        } else {
            //判断是否存在auth cookie
            if ($phpcms_auth) {
                $auth_key = $auth_key = get_auth_key('login');
                list($userid, $password) = explode("\t", sys_auth($phpcms_auth, 'DECODE', $auth_key));

                //验证用户，获取用户信息
                $this->memberinfo = $this->db->get_one(array('userid' => $userid));

                //获取用户模型信息
                $this->db->set_model($this->memberinfo['modelid']);

                $this->_member_modelinfo = $this->db->get_one(array('userid' => $userid));
                $this->_member_modelinfo = $this->_member_modelinfo ? $this->_member_modelinfo : array();
                $this->db->set_model();
                if (is_array($this->memberinfo)) {
                    $this->memberinfo = array_merge($this->memberinfo, $this->_member_modelinfo);
                }

                if ($this->memberinfo && $this->memberinfo['password'] === $password) {
                    //
                } else {
                    param::set_cookie('auth', '');
                    param::set_cookie('_userid', '');
                    param::set_cookie('_username', '');
                    param::set_cookie('_groupid', '');
                }
                unset($userid, $password, $phpcms_auth, $auth_key);
            } else {
                $forward = isset($_GET['forward']) ? urlencode($_GET['forward']) : urlencode(get_url());

                $go_url = 'index.php?m=member&c=index&a=login&forward=' . $forward;
                //success_m('请先登录', 'index.php?m=member&c=index&a=login&forward=' . $forward);
                header('Location:' . $go_url);
                exit();
            }
        }
    }

    /**
     *
     * IP禁止判断 ...
     */
    final private function check_ip() {
        $this->ipbanned = pc_base::load_model('ipbanned_model');
        $this->ipbanned->check_ip();
    }

}